﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.events.*;
    import flash.utils.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class InitWorldCommand extends Command {

		[Inject]
        public var event:InfoEvent;
		[Inject]
        public var ticker:TickModel;
		[Inject]
        public var world:WorldModel;
		[Inject]
        public var factory:WorldFactory;
		[Inject]
        public var slots:RoomSlotsModel;
		[Inject]
        public var sessionModel:SessionModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var gameStateModel:GameStateModel;

        override public function execute():void{
            this.gameStateModel.initWorldComplete = false;
            this.slots.resetRoomSlotsModel();
            this.initWorld();
        }
        private function initWorld():void{
            DoctorVO.canEat = false;
            this.ticker.reset();
            this.world.reset();
            this.preloadRoomTypes(this.event.info.rooms);
            this.preloadRoomIcons();
        }
        private function preloadRoomIcons():void{
            var _local1:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST);
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_FOUNTAIN.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_KIOSK.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_KINDERGARTEN.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_REHA_FUN.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_TREATMENT_COSMETIC.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_REHA_SPA.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_TREATMENT_MENTAL.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_REHA_SPORT.toLowerCase());
            _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, GameConstants.TYPE_TREATMENT_PHYSICAL.toLowerCase());
            dispatch(_local1);
        }
        private function preloadRoomTypes(_arg1:Array):void{
            var _local3:Object;
            var _local4:Object;
            var _local5:Boolean;
            var _local2:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, this.onPreloaded, GameConstants.PRELOAD_HOSPITAL);
            for each (_local3 in _arg1) {
                _local2.addAssetRequest(ProtocolConstants.LOAD_ROOM, _local3.room_type.toLowerCase());
                for each (_local4 in _local3.elements) {
                    _local2.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, _local4.element_type.toLowerCase());
                };
                _local5 = false;
				if(_local3.pets.length==2){
					trace("hello");
				}
                switch (_local3.room_type){
                    case GameConstants.TYPE_ENTRANCE_AREA:
                    case GameConstants.TYPE_RECEPTION:
                    case GameConstants.TYPE_WAITING_ROOM:
                    case GameConstants.TYPE_KINDERGARTEN:
                    case GameConstants.TYPE_FOUNTAIN:
					case GameConstants.TYPE_TREATMENT_GENERAL:
                        _local5 = true;
                        break;
                };
                this.addPatientTypeRequests(_local3.pets, _local2, _local5);
            };
            dispatch(_local2);
        }
        private function addPatientTypeRequests(_arg1:Array, _arg2:RequestEvent, _arg3:Boolean):void{
            var _local4:Object;
            var _local5:String;
            var _local6:String;
			if(_arg1.length==2){
				trace("hello");
			}
            for each (_local4 in _arg1) {
                if ((((_local4.workflow_state == GameConstants.STATE_DOCTOR)) || (_arg3))){
                    _local5 = (_local4.pet_type + "_");
                    switch (_local4.workflow_state){
                        case GameConstants.STATE_DOCTOR:
                            _local6 = ProtocolConstants.LOAD_DOCTOR;
                            _local5 = (_local5 + _local4.workflow_state);
                            break;
                        case WorldConstants.BABY:
                            _local5 = (_local5 + _local4.workflow_state);
                            _local6 = ProtocolConstants.LOAD_BABY;
                            break;
                        default:
                            _local5 = (_local5 + _local4.disease_name);
                            _local6 = ProtocolConstants.LOAD_PATIENT;
                            break;
                    };
                    _arg2.addAssetRequest(_local6, _local5.toLowerCase());
                };
            };
        }
        private function onPreloaded(_arg1:ResultVO):void{
            this.initAreas();
            this.initRooms(this.event.info.rooms);
            this.initAvatar();
            this.initWaitingQueue();
            setTimeout(this.dispatchInitWorld, 1000);
        }
        private function dispatchInitWorld():void{
            dispatch(new Event(EventConstants.INIT_WORLD_COMPLETE));
        }
        private function initAreas():void{
            var _local1:Object = {};
            _local1[ProtocolConstants.ID] = WorldConstants.BUILDING;
            _local1[ProtocolConstants.ROOM_TYPE] = WorldConstants.BUILDING;
            var _local2:IArea = this.factory.createArea(_local1);
            this.world.addArea(_local2);
        }
        private function initRooms(_arg1:Array):void{
            this.createRooms(_arg1);
            this.createActorsOfRooms(_arg1);
        }
        private function createRooms(_arg1:Array):void{
            var _local2:Object;
            var _local3:InfoEvent;
            for each (_local2 in _arg1) {
                _local3 = new InfoEvent(EventConstants.CREATE_ROOM, null, {room:_local2});
                if (_local2[ProtocolConstants.ROOM_TYPE] == "Kitchen"){
                    _local2[ProtocolConstants.ROOM_TYPE] = GameConstants.KIOSK;
                };
                dispatch(_local3);
                dispatch(new InfoEvent(EventConstants.SHOW_REWARD_ICONS, _local2.id, false));
            };
        }
        private function createActorsOfRooms(_arg1:Array):void{
            var _local2:Object;
            for each (_local2 in _arg1) {
                this.createActors(((_local2.pets) || ([])), _local2.id);
            };
        }
        private function createActors(_arg1:Array, _arg2:String):void{
            var _local3:Object;
            var _local4:String;
            var _local5:Boolean;
            var _local6:String;
            var _local7:String;
            var _local8:String;
            var _local9:InfoEvent;
            var _local10:String;
            for each (_local3 in _arg1) {
                _local4 = _local3[ProtocolConstants.ROOM_ID];
                _local5 = (_local4 == _arg2);
                if (_local5){
                    if (_local3[ProtocolConstants.ACTOR_TYPE]){
                        _local7 = _local3[ProtocolConstants.STATE];
                        _local8 = _arg2;
                        switch (_local7){
                            case WorldConstants.DOCTOR:
                                _local6 = EventConstants.CREATE_DOCTOR;
                                break;
                            case WorldConstants.BABY:
                                _local6 = EventConstants.CREATE_BABY;
                                break;
                            case WorldConstants.CURED:
                                _local6 = EventConstants.DISMISS_CURED_PATIENT;
                                _local8 = _local3.id;
                                break;
                            default:
                                _local6 = EventConstants.CREATE_PATIENT;
                                break;
                        };
                        if (!_local3.needs_welcome){
                            _local9 = new InfoEvent(_local6, _local8, _local3);
                            dispatch(_local9);
                        };
                    };
                } else {
                    _local10 = ((((("user " + this.userModel.mySocialID) + " has an actor of the corrcet room ") + _local4) + " also in the wrong room ") + _arg2);
                    dispatch(new InfoEvent(EventConstants.REPORT_PROBLEM, _local10));
                };
            };
        }
        private function initAvatar():void{
            var _local1:Object = {};
            _local1[ProtocolConstants.ID] = GameConstants.AVATAR_ID;
            _local1[ProtocolConstants.STATE] = WorldConstants.AVATAR;
            _local1[ProtocolConstants.ACTOR_TYPE] = "Avatar";
            var _local2:InfoEvent = new InfoEvent(EventConstants.CREATE_AVATAR, null, _local1);
            dispatch(_local2);
        }
        private function initWaitingQueue():void{
            if (!this.userModel.isVisitFriendsMode()){
                dispatch(new Event(EventConstants.INIT_WAITING_QUEUE));
            };
        }

    }
}//package net.wooga.pets.controllers.world 
